package cn.edu.thu.tsquality.core.algorithm.window.mean;


import cn.edu.thu.tsquality.adaptor.single.SingleAdaptor;
import cn.edu.thu.tsquality.core.common.algorithm.IAlgorithm;
import cn.edu.thu.tsquality.core.common.util.ArgumentsHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class SingleMain
{
    public static void main(String[] args) throws IOException
    {

        System.out.println("WindowMean");
        System.out.println("args length="+args.length);
        for(int i = 0; i < args.length; i++)
            System.out.println("arg[" + i + "] = " + args[i]);

        String inputPath = args[0];
        String outputPath = args[1];

        Integer windowSize = 4;
        List<String> usedAttrs = null;

        try
        {
            String[] arguments = ArgumentsHelper.getArguments(args[2]);

            int argIndex = 0;

            if(arguments.length != 1 && arguments.length != 2)
                throw new IllegalArgumentException("argument error, expected 1/2 argument, got "+args.length);

            // 如果参数有1个，那么第一个参数是滑动平均窗口长度。以整张表作为输入，进行不精确的修复。
            // 如果参数有2个，那么第一个参数是选择列名称，第二个参数是窗口长度。选择列，进行不完整的修复。
            if(arguments.length == 2)
            {
                String[] attrs = ArgumentsHelper.getValues(arguments[argIndex++]);
                if(attrs.length != 1)
                    throw new IllegalArgumentException("attrs error, expected 1 attr, got "+attrs.length);

                usedAttrs = new ArrayList<>(Arrays.asList(attrs));
            }

            String[] algorithmArgs = ArgumentsHelper.getValues(arguments[argIndex++]);
            if(algorithmArgs.length != 1)
                throw new IllegalArgumentException("window size args error, expected 1 arg, got "+algorithmArgs.length);

            windowSize = Integer.parseInt(algorithmArgs[0]);


            IAlgorithm algorithm = new WindowMean(windowSize);
            SingleAdaptor adaptor = new SingleAdaptor();
            adaptor.setAlgorithm(algorithm);
            adaptor.run(inputPath, outputPath, usedAttrs);

        }
        catch (Exception e)
        {
            System.out.println("Catch Exception: "+e.toString());
            throw e;
        }
    }
}


